﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HandBook.Data;
using Telerik.Web.UI;
using HandBook.Common;
using System.Data.Linq.SqlClient;

public partial class FoodBook : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
        string[] namesArray = new string[] { "Tất Cả","A", "B", "C", 
             "D","Đ", "E", "F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","Y" };
        foreach (string name in namesArray)
        {
            LinkButton linkButton1 = new LinkButton();
            LiteralControl lc = new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            linkButton1.Text = name;
            linkButton1.CommandArgument = name;
            linkButton1.Command += new CommandEventHandler(linkButton1_Command);
            Panel1.Controls.Add(linkButton1);
            Panel1.Controls.Add(lc);
        }
    }

    void linkButton1_Command(object sender, CommandEventArgs e)
    {
        string value = e.CommandArgument.ToString();
        if (value == "Tất Cả")
        {
            GetAllFoodBook();
            foodBookRG.Rebind();
        }
        else
        {
            var foodBookList = from food in HandBookData.EatDrinkDB.FoodBooks
                               where food.Title.StartsWith(value)
                               orderby food.Title ascending
                               select food;
            foodBookRG.DataSource = foodBookList;
            foodBookRG.Rebind();
        }
    }

    protected void foodBookRG_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        GetAllFoodBook();
    }

    private void GetAllFoodBook()
    {
        var foodBook = from food in HandBookData.EatDrinkDB.FoodBooks
                       where food.IsShowHomePage == Convert.ToBoolean("True")
                       orderby food.Title ascending
                       select food;
        foodBookRG.DataSource = foodBook;
    }
    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        Label1.Visible = false;
        string tenMonan = txtSearch.Text.Trim();

        if (tenMonan == "Nhập Tên Món Ăn Cần Tìm...")
        {
            Label1.Visible = true;
        }
        else
        {
            char[] separator = new char[] {' '};

            string[] strSplitArr = tenMonan.Split(separator);
            foreach (string arrStr in strSplitArr)
            {
                var foodBookList = from food in HandBookData.EatDrinkDB.FoodBooks
                                   where food.Title.StartsWith(arrStr) || food.Title.Contains(arrStr)
                                   select food;
                foodBookRG.DataSource = foodBookList;
                foodBookRG.Rebind();
            }
            
        }
    }
}
